package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 本类用来演示:
 *
 * @Author 胡英俊
 * @Date 2022/7/13 8:20
 */
@RestController
public class TestRibbon {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;//轮训算法的
    @GetMapping("testRibbon")
    public String testRibbon(String serviceName){
        String forObject = restTemplate.getForObject("http://" + serviceName + "/get", String.class);
        System.out.println(forObject);
        return forObject;
    }

//    随机的,可以输出服务的东西
    @GetMapping("testRibbonRule")
    public String testRibbonRule(String serviceName){
        ServiceInstance choose = loadBalancerClient.choose(serviceName);
        return choose.toString();
    }
}
